﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._sujiahao;
using _2112A_2_MES.ChuBoWens;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.ChuBoWen
{
    /// <summary>
    /// 调拨转移表服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class WmTransferService: ApplicationService, IWmTransferRepository
    {
        /// <summary>
        /// 调拨转移仓储
        /// </summary>
        private readonly IRepository<WmTransfer, int> _wmTransferRepository;
        public WmTransferService(IRepository<WmTransfer, int> wmTransferRepository)
        {
            _wmTransferRepository = wmTransferRepository;
        }

        /// <summary>
        /// 获取调拨转移表
        /// </summary>
        /// <param name="transfername"></param>
        /// <param name="fromwarehousename"></param>
        /// <param name="towarehousename"></param>
        /// <param name="clientId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<InterSelect<object>> GetWmTransferList(string? transfername, string? fromwarehousename, string? towarehousename,string? clientId, int pageIndex, int pageSize)
        {
            var wmTransferList = await _wmTransferRepository.GetListAsync();
            if (!string.IsNullOrEmpty(transfername))
            {
                wmTransferList = wmTransferList.Where(p => p.TransferName.Contains(transfername)).ToList();
            }
            if (!string.IsNullOrEmpty(fromwarehousename))
            {
                wmTransferList = wmTransferList.Where(p => p.FromWarehouseName.Contains(fromwarehousename)).ToList();
            }
            if (!string.IsNullOrEmpty(towarehousename))
            {
                wmTransferList = wmTransferList.Where(p => p.ToWarehouseName.Contains(towarehousename)).ToList();
            }
            if (!string.IsNullOrEmpty(clientId))
            {
                wmTransferList = wmTransferList.Where(p => p.ClientId == clientId).ToList();
            }
            var totalCount = wmTransferList.Count;
            var pageList = wmTransferList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = pageList.MapToList<WmTransfer>();
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (wmTransferList != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增调拨转移
        /// </summary>
        /// <param name="wmTransfer"></param>
        /// <returns></returns>
        public async Task<int> AddWmTransfer(WmTransfer wmTransfer)
        {
            var result = await _wmTransferRepository.FindAsync(x => x.TransferCode == wmTransfer.TransferCode);
            if (result == null)
            {
                wmTransfer.CreateBy = "楚博文";
                wmTransfer.CreateTime = DateTime.Now;
                await _wmTransferRepository.InsertAsync(wmTransfer);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除调拨转移表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWmTransfer(int id)
        {
            var result = await _wmTransferRepository.FindAsync(id);
            if (result != null)
            {
                await _wmTransferRepository.DeleteAsync(id);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 获取调拨转移
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WmTransfer> GetWmTransfer(int id)
        {
            var result = await _wmTransferRepository.FindAsync(id);
            if (result != null)
            {
                return result;
            }
            return null;
        }

        /// <summary>
        /// 修改调拨转移
        /// </summary>
        /// <param name="wmTransferDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateWmTransfer(WmTransferDTO wmTransferDTO)
        {
            var result = await _wmTransferRepository.GetAsync(wmTransferDTO.Id);
            result.TransferCode = wmTransferDTO.TransferCode;
            result.TransferName = wmTransferDTO.TransferName;
            result.ClientId = wmTransferDTO.ClientId;
            result.FromWarehouseName = wmTransferDTO.FromWarehouseName;
            result.ToWarehouseName = wmTransferDTO.ToWarehouseName;
            result.TransferDate = wmTransferDTO.TransferDate;
            result.Status = wmTransferDTO.Status;
            result.UpdateBy = "小呆呆";
            result.UpdateTime = DateTime.Now;
            await _wmTransferRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
